<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="房间名称" prop="name">
                <el-input
                  class="w-200"
                  v-model.trim="queryParams.name"
                  placeholder="请输入房间名称"
                  @keyup.enter.native="handleQuery"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="房间楼层" prop="floorId">
                <el-select
                  v-model="queryParams.floorId"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in floorList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="房间分类" prop="typeId">
                <el-select
                  v-model="queryParams.typeId"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in sortList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="房间床位" prop="bedsId">
                <el-select
                  v-model="queryParams.bedsId"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in bedList"
                    :key="item.id"
                    :label="item.value"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="房间状态" prop="state">
                <el-select
                  v-model="queryParams.state"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in RoomStateOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 房间列表 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">房间列表</div>
        <div class="op_right">
          <button class="primary_button" @click="AddRoom">添加房间</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="RoomList" stripe style="width: 100%">
          <el-table-column prop="reorder" label="房间排序"> </el-table-column>
          <el-table-column prop="name" label="房间名称"> </el-table-column>
          <el-table-column prop="configName" label="配置名称">
            <template slot-scope="scope">
              <div
                v-for="(item, index) in scope.row.roomConfigList"
                :key="index"
              >
                {{ item.configName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="floor" label="房间楼层"> </el-table-column>
          <el-table-column prop="typeName" label="房间分类"> </el-table-column>
          <el-table-column prop="beds" label="房间床位"> </el-table-column>
          <el-table-column prop="state" label="房间状态">
            <template slot-scope="scope">
              <div>{{ RoomStateObj[scope.row.state] }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="300px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="Edit(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                :disabled="
                  scope.row.state === 1 || scope.row.state === '1'
                    ? true
                    : false
                "
                @click="Takedown(scope.row, 1)"
              >
                <span class="separate_left">|</span>开启<span
                  class="separate_right"
                  >|</span
                ></el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="Takedown(scope.row, 0)"
                :disabled="
                  scope.row.state === 0 || scope.row.state === '0'
                    ? true
                    : false
                "
                >关闭</el-button
              >
              <el-button size="mini" type="text" @click="DelSort(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 添加房间 -->
      <el-dialog
        :title="title"
        :visible.sync="newRoomdialogVisible"
        width="400px"
        :before-close="CancelRoomSubmit"
        class="no-dialog-padding"
      >
        <span>
          <div class="add_room">
            <el-form
              ref="newRoomForm"
              :rules="newRoomRules"
              :model="newRoomForm"
            >
              <el-row :gutter="40">
                <el-col :span="24">
                  <el-form-item label="排序号" prop="reorder">
                    <el-input-number
                      class="w-100 text-left"
                      placeholder="请输入排序号"
                      v-model="newRoomForm.reorder"
                      :min="0"
                      :step="1"
                      :precision="0"
                      :controls="false"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间名称" prop="name">
                    <el-input
                      class="w-100"
                      v-model="newRoomForm.name"
                      placeholder="请输入房间名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间楼层" prop="floorId">
                    <el-select
                      v-model="newRoomForm.floorId"
                      placeholder="请选择房间楼层"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in floorList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间分类" prop="typeId">
                    <el-select
                      v-model="newRoomForm.typeId"
                      placeholder="请选择"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in sortList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间床位" prop="bedsId">
                    <el-select
                      v-model="newRoomForm.bedsId"
                      placeholder="请选择房间床位"
                      class="w-100"
                    >
                      <el-option
                        v-for="item in bedList"
                        :key="item.id"
                        :label="item.value"
                        :value="item.id"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="房间配置" prop="configIdList">
                    <el-select
                      v-model="newRoomForm.configIdList"
                      placeholder="请选择房间配置"
                      class="w-100"
                      multiple
                    >
                      <el-option
                        v-for="item in AllRoomConfig"
                        :key="item.configId"
                        :label="item.configName"
                        :value="item.configId"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="CancelRoomSubmit"
            >取 消</el-button
          >
          <el-button
            type="primary"
            @click="RoomSubmit"
            :loading="newRoomLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
  
  <script>
import {
  getRoomList,
  CreateRoom,
  UpdateRoom,
  DeleteRoom,
  closeRoom,
  getSelectList,
  getAllRoomsConfigPage,
} from "@/api/business/room";
export default {
  name: "EryuoaStoreRoomList",

  data() {
    return {
      AllRoomConfig: [], // 所有房间配置
      newRoomdialogVisible: false, // 新增房间
      newRoomLoading: false,
      loading: false,
      title: "",
      RoomList: [], // 房间列表数据
      floorList: [], // 楼层列表
      sortList: [], // 楼层列表
      bedList: [], // 楼层列表

      // options: [
      //   {
      //     value: "选项1",
      //     label: "黄金糕",
      //   },
      // ],

      RoomStateOptions: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "关闭",
        },
        {
          value: "1",
          label: "开启",
        },
      ],
      RoomData: [], // 客户来源数据

      // 房间状态
      RoomStateObj: {
        0: "关闭",
        1: "开启",
      },

      typeObj: {
        1: "充值",
        2: "消费",
        3: "钱包",
        4: "调整",
      },

      newRoomRules: {
        reorder: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        name: [{ required: true, message: "请输入房间名称", trigger: "blur" }],
        // floorId: [
        //   { required: true, message: "请选择房间楼层", trigger: "blur" },
        // ],

        typeId: [
          { required: true, message: "请选择房间分类", trigger: "blur" },
        ],
        bedsId: [
          { required: true, message: "请选择房间床位", trigger: "blur" },
        ],
        // configIdList: [
        //   { required: true, message: "请选择房间配置", trigger: "blur" },
        // ],
      },

      newRoomForm: {
        reorder: "", // 排序
        name: "",
        bedsId: "",
        floorId: "",
        typeId: "", // 房间类型id
        typeName: "",
        configIdList: [], // 房间配置id列表
      },

      value: "",
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: "",
        floorId: "",
        typeId: "",
        bedsId: "",
        state: null,
      },

      // 性别
      Sexoptions: [
        {
          value: 0,
          label: "未知",
        },
        {
          value: 1,
          label: "男",
        },
        {
          value: 2,
          label: "女",
        },
      ],
      total: 0,
    };
  },

  created() {
    this.getList();
    this.getSelect(1);
    this.getSelect(2);
    this.getSelect(3);
  },

  mounted() {},

  methods: {
    getSelect(type) {
      getSelectList({ type: type }).then((res) => {
        const { code, data } = res;
        if (code === 0) {
          if (type === 1) {
            this.floorList = data;
          } else if (type === 2) {
            this.sortList = data;
          } else if (type === 3) {
            this.bedList = data;
          }
        }
      });
    },

    // 添加房间
    AddRoom() {
      this.newRoomdialogVisible = true;
      this.title = "添加房间";
      this.getAllRoomsConfigList();
    },

    // 获取所有房间配置
    getAllRoomsConfigList() {
      getAllRoomsConfigPage()
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.AllRoomConfig = data;
          }
        })
        .catch((err) => {});
    },

    // 表单重置
    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },

    // 新增房间 弹框 确认
    RoomSubmit() {
      this.$refs["newRoomForm"].validate((valid) => {
        if (valid) {
          if (!this.newRoomForm.roomId) {
            this.NewRoom();
          } else {
            this.EditRoom();
          }
        } else {
        }
      });
    },

    // 新增房间
    NewRoom() {
      CreateRoom(this.newRoomForm)
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelRoomSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑房间
    EditRoom() {
      const {
        name,
        reorder,
        floorId,
        bedsId,
        typeId,
        typeName,
        roomId,
        configIdList,
      } = this.newRoomForm;
      UpdateRoom({
        name: name,
        reorder: reorder,
        floorId: floorId,
        bedsId: bedsId,
        typeId: typeId,
        typeName: typeName,
        roomId: roomId,
        configIdList: configIdList,
      })
        .then((res) => {
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.CancelRoomSubmit();
          }
        })
        .catch((err) => {});
    },

    // 编辑房间
    Edit(row) {
      this.newRoomdialogVisible = true;
      this.getAllRoomsConfigList();
      this.title = "编辑房间";
      this.$nextTick(() => {
        let Item = JSON.parse(JSON.stringify(row));
        this.newRoomForm.name = Item.name;
        this.newRoomForm.reorder = Item.reorder;
        this.newRoomForm.floorId = Item.floorId;
        this.newRoomForm.bedsId = Item.bedsId;
        this.newRoomForm.typeId = Item.typeId;
        this.newRoomForm.roomId = Item.roomId;
        this.newRoomForm.configIdList = Item.roomConfigList.map((item) => {
          return item.configId;
        });
      });
    },

    // 取消 弹框 关闭
    CancelRoomSubmit() {
      this.newRoomdialogVisible = false;
      this.newRoomForm.roomId = "";
      this.resetForm("newRoomForm");
    },

    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 房间列表
    getList() {
      this.loading = true;
      getRoomList(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.RoomList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 开启 关闭
    Takedown(row, state) {
      this.$confirm(
        "是否确认" + (state === 0 ? "关闭" : "开启") + "该房间?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          closeRoom({
            id: row.roomId,
          }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.getList();
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            }
          });
        })
        .catch(() => {});
    },

    // 商品列表 删除
    DelSort(row) {
      this.DeleteRoomList(row);
    },

    DeleteRoomList(row) {
      this.$confirm("房间删除后无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteRoom({ id: row.roomId })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getList();
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              }
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.getList();
    },

    CancelRoomSubmit() {
      this.resetFormData("newRoomForm");
      this.$nextTick(() => {
        this.newRoomdialogVisible = false;
        this.newRoomLoading = false;
      });
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
  margin-left: 15px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}

.separate_left {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-right: 12px;
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
}

.add_room {
  margin: 0 40px;
}
</style>